#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020/9/6 10:41|10:41
# @Author  : yangdingyi
# @File    : 2020-09-06.107.二叉树的层次遍历II
# @Software: PyCharm
from tools.my_methods import *


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        ans = []
        if not root:
            return ans

        def dfs(rt, d):
            if rt:
                if d >= len(ans):
                    ans.append([])
                ans[d].append(rt.val)

            else:
                return
            if rt.left:
                dfs(rt.left, d+1)
            if rt.right:
                dfs(rt.right, d+1)

        dfs(root, 0)
        ans.reverse()
        return ans


# data = [3, 9, 20, None, None, 15, 7]
#
# rt0 = mk_tree(data, TreeNode)
# display(rt0)

questions = [
    ([3, 9, 20, None, None, 15, 7], ),
]

work(questions, lambda q: Solution().levelOrderBottom(mk_tree(q[0], TreeNode)))

